package com.example.zx.theline.jclr;


import android.content.Context;
import android.os.IBinder;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.zx.theline.R;

import Utils.BackHandlerHelper;
import app.MyApplication;
import base.BaseActivity;

/**
 * Changed by Jianqi on 2017\11\2.
 * <p>
 * 修改检查录入主界面
 */
public class JclrActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1, mRadioButton2, mRadioButton3;
    private Fragment mCustodyListFragment, mCustodyListDclFragment, mCustodyListYclFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jclr);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mRadioButton1 = (RadioButton) findViewById(R.id.lrxx);
        mRadioButton2 = (RadioButton) findViewById(R.id.dcl);
        mRadioButton3 = (RadioButton) findViewById(R.id.ycl);
    }

    @Override
    protected void InitData() {
        if (MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton1, 0, 0, 0, 0);
        } else if (MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && !MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton1, 0, 0, 8, 0);
        } else if (MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && !MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton1, 0, 8, 0, 0);
        } else if (!MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton2, 8, 0, 0, 1);
        } else if (!MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && !MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton3, 8, 8, 0, 2);
        } else if (!MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && !MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton2, 8, 0, 8, 1);
        } else if (MyApplication.getmLoginUser().getJdjc().isJdjclrxx() && !MyApplication.getmLoginUser().getJdjc().isJdjcdcl() && !MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
            displayView(mRadioButton1, 0, 8, 8, 0);
        }
    }

    @Override
    protected void InitListen() {
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.lrxx:
                setSelect(0);
                break;
            case R.id.dcl:
                setSelect(1);
                break;
            case R.id.ycl:
                setSelect(2);
                break;
        }
    }

    //控制界面中按钮的显示及默认选中状态
    private void displayView(RadioButton mRadioBtn, int a, int b, int c, int d) {
        mRadioButton1.setVisibility(a);
        mRadioButton2.setVisibility(b);
        mRadioButton3.setVisibility(c);
        mRadioBtn.setChecked(true);
        setSelect(d);
    }

    //Fragment的选择事件
    private void setSelect(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                //录入信息
                if (MyApplication.getmLoginUser().getJdjc().isJdjclrxx()) {
                    mRadioButton1.setVisibility(View.VISIBLE);
                    if (mCustodyListFragment == null) {
                        mCustodyListFragment = new CustodyListFragment();
                        transaction.add(R.id.framelayout, mCustodyListFragment);
                    }
                } else {
                    mRadioButton1.setVisibility(View.GONE);
                }
                transaction.show(mCustodyListFragment);
                break;
            case 1:
                //待处理
                if (MyApplication.getmLoginUser().getJdjc().isJdjcdcl()) {
                    mRadioButton2.setVisibility(View.VISIBLE);
                    if (mCustodyListDclFragment == null) {
                        mCustodyListDclFragment = new CustodyListDclFragment();
                        transaction.add(R.id.framelayout, mCustodyListDclFragment);
                    }
                } else {
                    mRadioButton2.setVisibility(View.GONE);
                }
                transaction.show(mCustodyListDclFragment);
                break;
            case 2:
                //已处理
                if (MyApplication.getmLoginUser().getJdjc().isJdjcycl()) {
                    mRadioButton3.setVisibility(View.VISIBLE);
                    if (mCustodyListYclFragment == null) {
                        mCustodyListYclFragment = new CustodyListYclFragment();
                        transaction.add(R.id.framelayout, mCustodyListYclFragment);
                    }
                } else {
                    mRadioButton3.setVisibility(View.GONE);
                }
                transaction.show(mCustodyListYclFragment);
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (mCustodyListFragment != null) {
            transaction.hide(mCustodyListFragment);
        }
        if (mCustodyListDclFragment != null) {
            transaction.hide(mCustodyListDclFragment);
        }
        if (mCustodyListYclFragment != null) {
            transaction.hide(mCustodyListYclFragment);
        }
    }

    //处理系统返回键
    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}
